import MySQLdb
import datetime
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

import market
import numpy as np

hours    = mdates.HourLocator()   # every year
days   = mdates.DayLocator()  # every month
dayFmt = mdates.DateFormatter('%m-%d')


historyPriceByHour = market.historyPriceNum()
print historyPriceByHour

dates = [i[0] for i in historyPriceByHour]
prices = [i[1] for i in historyPriceByHour]
amounts = [i[2] for i in historyPriceByHour]



fig = plt.figure(1)
ax1 = fig.add_subplot(211)
ax1.plot_date(dates,prices,'ro-',lw=2,ls='steps-post')
plt.ylabel('Stock Price')

ax1.xaxis.set_major_locator(days)
ax1.xaxis.set_major_formatter(dayFmt)
ax1.xaxis.set_minor_locator(hours)
ax1.grid(True)

ax2 = fig.add_subplot(212)
fillcolor = 'r'
ax2.bar(dates, amounts, width=0.05, linewidth=0)
plt.ylabel('Trade Volume')
plt.xlabel('Time')

ax2.xaxis.set_major_locator(days)
ax2.xaxis.set_major_formatter(dayFmt)
ax2.xaxis.set_minor_locator(hours)
ax2.grid(True)

fig.autofmt_xdate()
plt.savefig('/home/naich/web/static/price-volume.png',dpi=100)
